package com.example.qxfw.mongodb.hy.controller;

import com.example.qxfw.common.SysResult;
import com.example.qxfw.mongodb.hy.entity.HyDocFile;
import com.example.qxfw.mongodb.hy.service.DocFileService;
import com.example.qxfw.mongodb.hy.service.impl.DocFileServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.nio.file.Files;

@Api(tags = "Mongo服务库-文档DocFile")
@RestController
@CrossOrigin()
@RequestMapping("/hy/docFile")
public class DocFileController {

    @Autowired
    private DocFileService docFileService;

    @ApiOperation("获取行业数据DocFile")
    @GetMapping("/getDocFileById")
    public void getDocFileById(String id, HttpServletResponse response) throws IOException {
        HyDocFile hyDocFile = docFileService.getByTableId(id);
        InputStream inputStream = new ByteArrayInputStream(hyDocFile.getData());
        response.setContentType("application/octet-stream");
        response.addHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode("doc_File", "UTF-8"));
        ServletOutputStream outputStream = response.getOutputStream();
        byte[] b = new byte[1024];
        int len;
        while ((len = inputStream.read(b)) > 0) {
            outputStream.write(b, 0, len);
        }
        inputStream.close();
    }

    @ApiOperation("根据id和产品名获取行业数据")
    @GetMapping("/getDocFileByIdAndName")
    public void getDocFileByIdAndName(String id,String name,HttpServletResponse response) throws IOException{
        HyDocFile hyDocFile = docFileService.getByNameAndId(name, id);
        InputStream inputStream = new ByteArrayInputStream(hyDocFile.getData());
        response.setContentType("application/octet-stream");
        response.addHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(name+".docx", "UTF-8"));
        ServletOutputStream outputStream = response.getOutputStream();
        byte[] b = new byte[1024];
        int len;
        while ((len = inputStream.read(b)) > 0) {
            outputStream.write(b, 0, len);
        }
        inputStream.close();
    }

    @ApiOperation("根据_id删除文档")
    @GetMapping("/deleteDocFile")
    public SysResult deleteDocFile(String id){
        Long rows = docFileService.deleteById(id);
        return SysResult.success(rows);
    }

    public static void main(String[] args) {

        try {
            File docxFile = new File("");

            // 检查文件是否存在
            if (!docxFile.exists()) {
                System.err.println("File not found.");
                return;
            }

            // 读取DOCX文件为byte[]
            byte[] docxBytes = Files.readAllBytes(docxFile.toPath());
            HyDocFile hyDocFile = new HyDocFile();
            hyDocFile.setData(docxBytes);
            DocFileService docFileService1 = new DocFileServiceImpl();
            docFileService1.save(hyDocFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}
